<?php
class Manage_admin extends CI_Controller{
	private $per_page_index = 1;
	private $per_page_detail = 1;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn() ) redirect("/");
		if($this->session->userdata("type_user") != "admin") redirect("/");
		$this->load->model("model_user");
	}
	function index(){
		$embedString = "";
		$offset = 0;
		$data['title'] = TITLE.' - Atur Pegawai';
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "admin_manage_admin.js";
		$data['active_admin'] = 'y';
		if($this->input->post("btnregister") ){
			$redirect = $this->_register();
			if($redirect) $data['register_success'] = "y";
			$this->form_validation->field_data = array();
		}
		$params['order_by'] = "username asc, user_id desc";
		$count = count($this->model_user->getAdminList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/manage_admin/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		//manipulate pagination link to embed the suffix needed
		$data['pagination'] = Paging::embedSuffixInPaging($embedString,$data['pagination']);
		
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['admin_list'] = $this->model_user->getAdminList($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = $embedString;
		$this->load->view('admin/manage_admin',$data);
	}
	function load($offset=0){
		$key = trim($this->input->get("q",true));
		$keyword_decode = rawurldecode($key);
		$embedString = '';
		if($key != ""){
			$embedString = '?q='.rawurlencode($key);
		}
		$params['keyword'] = $keyword_decode;
		
		$order_by = "username asc, user_id asc";
		
		$sort_by_field = $this->input->get("s",true);
		$sort_by_type = $this->input->get("t",true);
		
		if($this->input->get("s",true) ){
			$order_by = $sort_by_field . " " . $sort_by_type;
			$embedString = '?s='.$sort_by_field.'&t='.$sort_by_type;
		}
			
		
		$params['order_by'] = $order_by;
		$count = count($this->model_user->getAdminList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/manage_admin/load';
		$config['uri_segment'] = 4;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_index;
		
		$data['pagination'] = Paging::setPagination($config);
		//manipulate pagination link to embed the suffix needed
		$data['pagination'] = Paging::embedSuffixInPaging($embedString,$data['pagination']);
		
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['admin_list'] = $this->model_user->getAdminList($params);
		$data['per_page_index'] = $this->per_page_index;
		$data['embedString'] = $embedString;
		$this->load->view('admin/content_load/manage_admin_load',$data);
	}
	function detail($user_id="",$offset=0){
		if($user_id == "") redirect("admin/manage_admin");
		$data['active_admin'] = 'y';
		$data['title'] = TITLE.' - Admin Detail';
		$data['javascript'][] = "ajax_paging.js";
		$data['javascript'][] = "admin_manage_admin.js";
		
		$user_detail = $this->model_user->getDetail($user_id);
		$data['user_detail'] = $user_detail;
		
		$this->load->model("model_transaction");
		$params['user_id'] = $user_id;
		$params['order_by'] = "f.entry_date desc";
		$count = count($this->model_transaction->getAdminTransactionFollowedList($params));
		
		$this->load->library('paging');
		$config['base_url']   = base_url().'admin/manage_admin/detail/'.$user_id;
		$config['uri_segment'] = 5;
		$config['total_rows'] = $count;
		$config['per_page']   = $this->per_page_detail;
		
		$data['pagination'] = Paging::setPagination($config);
		
		$params['limit_1'] = $this->per_page_index;
		$params['limit_2'] = $offset;
		$data['transaction_followed_list'] = $this->model_transaction->getAdminTransactionFollowedList($params);
		
		$this->load->view('admin/manage_admin_detail',$data);
	}
	function delete($admin_id=""){
		$this->model_user->delete($admin_id);
		redirect("admin/user/karyawan");
	}
	
	function _register(){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               array(
                     'field'   => 'username',
                     'label'   => 'Username',
                     'rules'   => 'trim|required|callback__checkusername|xss_clean'
                  ),
               array(
                     'field'   => 'password',
                     'label'   => 'Password',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               array(
                     'field'   => 'name',
                     'label'   => 'Name',
                     'rules'   => 'trim|required|xss_clean'
                  ),   
               array(
                     'field'   => 'email',
                     'label'   => 'Email',
                     'rules'   => 'trim|required|valid_email|callback__checkemail|xss_clean'
                  ),
			   array(
                     'field'   => 'phone',
                     'label'   => 'Phone',
                     'rules'   => 'trim|required|is_natural|xss_clean'
                  ),
			   array(
                     'field'   => 'address',
                     'label'   => 'Address',
                     'rules'   => 'trim|required|xss_clean'
                  )
            );

		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			//save to database..
			$this->model_user->register("user");
			return true;
		}
	}
	function _checkusername($str){
		$this->form_validation->set_message('_checkusername', 'Username already exists');
		$unique = $this->model_user->checkunique("username",$str);
		return $unique;
	}
	function _checkemail($str){
		$this->form_validation->set_message('_checkemail', 'Email already exists');
		$unique = $this->model_user->checkunique("email",$str);
		return $unique;
	}
}